﻿// <copyright file="PhotoPanel.xaml.cs" company="Taasss">Copyright (c) 2009 All Right Reserved</copyright>
// <author>Ben Vincent</author>
// <date>2010-02-21</date>
// <summary>PhotoPanel</summary>
namespace Fotofly.PhotoViewer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Windows.Threading;

    using Fotofly.PhotoViewer.FotoflyWebService;
    using System.Windows.Browser;

    public partial class PhotoPanel : UserControl
    {
        private bool highlightRegions = false;
        private ImagePanel imagePanel;
        private MapPanel mapPanel;

        private double viewWidth;
        private double viewHeigth;

        public PhotoPanel(double imageWidth, double imageHeight)
        {
            this.InitializeComponent();

            this.Width = imageWidth;
            this.Height = imageHeight;

            // Scale images down to the right size to add the nav control
            this.viewWidth = imageWidth - 30;
            this.viewHeigth = imageHeight - 30;

            this.viewCanvas.Width = this.viewWidth;
            this.viewCanvas.Height = this.viewHeigth;
        }

        public Visibility ShowFotoflyLogo
        {
            get
            {
                return this.logoCanvas.Visibility;
            }
        }

        public bool ShowPeopleControl
        {
            set
            {
                if (value)
                {
                    this.peopleIcon.Visibility = Visibility.Visible;
                    this.peopleSeperator.Visibility = Visibility.Visible;
                }
                else
                {
                    this.peopleIcon.Visibility = Visibility.Collapsed;
                    this.peopleSeperator.Visibility = Visibility.Collapsed;
                }
            }
        }

        public ImagePanel ImagePanel
        {
            get
            {
                // Init Image Panel
                if (this.imagePanel == null)
                {
                    this.imagePanel = new ImagePanel(this.viewWidth, this.viewHeigth);

                    // Set image as initial view
                    this.viewCanvas.Children.Add(this.imagePanel);

                    // Show Nav Icons
                    this.cameraIcon.Visibility = Visibility.Visible;
                    this.cameraSeperator.Visibility = Visibility.Visible;
                }

                return this.imagePanel;
            }
        }

        public MapPanel MapPanel
        {
            get
            {
                // Init Map Panel
                if (this.mapPanel == null && this.imagePanel != null)
                {
                    // Calculate Icon dimensions
                    double iconWidth = 100;
                    double iconHeight = iconWidth * (this.imagePanel.ImageHeight / this.imagePanel.ImageWidth);

                    this.mapPanel = new MapPanel(this.viewWidth, this.viewHeigth, this.imagePanel.ImageUri, iconWidth, iconHeight);

                    // Show Nav Icons
                    this.mapIcon.Visibility = Visibility.Visible;
                    this.mapSeperator.Visibility = Visibility.Visible;
                }
                else if (this.imagePanel == null)
                {
                    throw new Exception("Image Panel has not been configured");
                }

                return this.mapPanel;
            }
        }

        private void MapMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.viewCanvas.Children.Clear();
            this.viewCanvas.Children.Add(this.mapPanel);

            this.imagePanel.HighlightRegions = false;
        }

        private void ImageMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.viewCanvas.Children.Clear();
            this.viewCanvas.Children.Add(this.imagePanel);

            this.imagePanel.HighlightRegions = false;
        }

        private void PeopleMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.viewCanvas.Children.Clear();
            this.viewCanvas.Children.Add(this.imagePanel);

            // Toggle Regions
            this.highlightRegions = !this.highlightRegions;

            this.imagePanel.HighlightRegions = this.highlightRegions;
        }

        private void FotoflyLogoMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("http://www.tassography.com/fotofly"));
        }
    }
}
